<template>
  <el-form class="login-container" :rules="rules" :model="loginForm" v-loading="loading">
    <h3 class="login-title">职工工资管理系统</h3>
    <el-form-item prop="username">
      <el-input type="text" v-model="loginForm.username"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" v-model="loginForm.password"></el-input>
    </el-form-item>
    <el-form-item>
      <el-radio v-model="account_type" label="common">职工</el-radio>
      <el-radio v-model="account_type" label="super">管理员</el-radio>
    </el-form-item>
    <el-form-item style="width: 100%">
      <el-button type="primary" @click.native.prevent="loginIn">登录</el-button>
      <el-button type="primary" @click.native.passive="registe">注册</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { postRequestByParams } from "../utils/api";

export default {
  data() {
    return {
      account_type: "common", //账号类型选择，默认是普通用户
      loginForm: {
        username: null,
        password: null
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 8, max: 20, message: "用户名长度不正确", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      loading: false
    };
  },

  methods: {
    loginIn: function() {
      var _this = this;
      this.loading = true;
      postRequestByParams("/account/login-super", {
        username: this.loginForm.username,
        password: this.loginForm.password
      }).then(resp => {
        _this.loading = false;
        if (resp.status == 200) {
          //成功
          var json = resp.data;
          if (json.message == "success" && this.account_type == "super") {
            _this.$router.replace({ path: "/super-admin" });
          } else if (json.message == "fail" && this.account_type == "super") {
            _this.$message.error("没有权限");
          } else if (json.message == "fail" && this.account_type == "common") {
            sessionStorage.setItem("username", this.loginForm.username);
            _this.$router.replace({ path: "/user" });
          } else {
            _this.$message.error("账号不存在或者密码错误");
          }
        } else {
          _this.$message.error("登录失败!", "失败!");
        }
      });
    },
    registe: function() {
      var _this = this;
      _this.$router.replace({ path: "/registe" });
    }
  }
};
</script>

<style>
.login-container {
  border-radius: 30px;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login-title {
  margin: 0px auto 40px auto;
}
</style>
